package eva

import (
	"context"
	"trusteva/app/eva/model"

	"trusteva/app/eva/internal/svc"
	"trusteva/app/eva/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddWhiteListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddWhiteListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddWhiteListLogic {
	return &AddWhiteListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddWhiteListLogic) AddWhiteList(req *types.AddWhiteListRo) (resp *types.AddWhiteListRsp, err error) {
	whitelist := &model.Whitelist{
		AppName:  req.AppName,
		AppId:    req.AppId,
		DeviceId: req.DeviceId,
		Memo:     req.Memo,
	}
	err = l.svcCtx.DB.WithContext(l.ctx).Create(whitelist).Error
	if err != nil {
		return nil, err
	}

	err = l.svcCtx.RedisClient.Set(whitelist.Key(), whitelist.Value())
	if err != nil {
		logx.Error("Set", whitelist.Key(), err)
		return nil, err
	}
	return
}
